home *** CD-ROM | disk | FTP | other *** search
- Path: zeus.rbi.informatik.uni-frankfurt.de!gna1pc
- From: Ferber@zoology.uni-frankfurt.de (Michael Ferber)
- Newsgroups: comp.lang.c++
- Subject: Re: write binary data to a file
- Date: Thu, 01 Feb 96 11:33:44 GMT
- Organization: Uni Frankfurt
- Message-ID: <4f54g8$sga@zeus.rbi.informatik.uni-frankfurt.de>
- NNTP-Posting-Host: gna1pc.zoologys.uni-frankfurt.de
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- Return-Path: <alistair@articad.demon.co.uk>
- Delivery-Date: Mon, 5 Feb 1996 15:28:08 +0100
- Date: Mon, 5 Feb 1996 12:48:36 +0000
- To: Michael Ferber <Ferber@zoology.uni-frankfurt.de>
- From: Alistair Imrie <alistair@articad.demon.co.uk>
- Subject: Re: write binary data to a file
-
- Dear Michael,
-
- I have encountered exactly the problem you describe. Here is the
- solution I use:
-
- Include the following filemcro.h file in any file where you want to
- write binary data:
-
- /*BEGIN FILE*/
-
- #if !defined __FILEMCRO_H
- #define __FILEMCRO_H
-
- #include <fstream.h>
-
- #define BinaryRead(item) read((char *)&item, sizeof(item))
- #define BinaryWrite(item) write((char *)&item, sizeof(item))
-
- #endif // __FILEMCRO_H
-
- /*END FILE*/
-
- To use the macros, copy the technique used in this example:
-
- void WriteTheseVariables(ostream& os, int var1, float var2, char var3)
- {
- os.BinaryWrite(var1);
- os.BinaryWrite(var2);
- os.BinaryWrite(var3);
- }
-
- void ReadTheseVariables(istream& is, int& var1, float& var2, char& var3)
- {
- is.BinaryRead(var1);
- is.BinaryRead(var2);
- is.BinaryRead(var3);
- }
-
- Hope this helps.
-
- You are welcome to post this back to the newsgroup. I'm a bit thick and
- don't understand how to reply to news items, only how to email the
- author.
- _ _
- / | / \ ' |_ _ ' __
- | | \ / | |\ | / | | |
- \_|__X__|_/_|_|\_\_|_|_|
-
- Alistair Imrie EMail alistair@articad.demon.co.uk
-
- ------------------------------------------------------------------------------------
- || Dr. Michael Ferber ||
- || Universitaet Frankfurt ||"science moves,
- || Zoologisches Institut || but slowly, slowly ......"
- || email: Ferber@zoology.uni-frankfurt.de || Tennyson
- ------------------------------------------------------------------------------------
-